package com.solidtrust.crawler.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.solidtrust.crawler.framework.ISitemap;
import com.solidtrust.crawlers.Che168Sitemap;
import com.solidtrust.crawlers.GanjiSitemap;
import com.solidtrust.crawlers.RenrencheSitemap;

@Controller
@RequestMapping("crawl")
public class CrawlerController {

	private static final Logger log4j = Logger.getLogger(CrawlerController.class);
	@Autowired RenrencheSitemap rrc;
	@Autowired Che168Sitemap che168;
	@Autowired GanjiSitemap ganji;
	@Autowired ThreadPoolTaskExecutor taskExecutor;
	private List<ISitemap> sitemaps = new ArrayList<>();
	
	private void register(){
		if(sitemaps.isEmpty()){
			sitemaps.add(rrc);
			sitemaps.add(che168);
			sitemaps.add(ganji);
		}
	}
	
	@Scheduled(cron = "0 0 20 * * *") //20:00 daily
	public void scheduleCrawl(){
		doCrawl(null);
	}
	
	@RequestMapping("start")
	public void crawl(Integer index){
		doCrawl(index);
	}
	
	@RequestMapping("price")
	public void price(Integer index){
		getPrice(index);
	}
	
	private void doCrawl(Integer index){
		register();
		
		for (int i = 0; i < sitemaps.size(); i++) {
			if(index != null && i != index){
				continue;
			}
			
			final ISitemap iSitemap = sitemaps.get(i);
//			try {
//				log4j.info("=========> start " + iSitemap.getSource());
//				iSitemap.start();
//				log4j.info("=========> end " + iSitemap.getSource());
//			} catch (Exception e) {
//				log4j.info(e.getMessage(), e);
//			}
			
			taskExecutor.execute(new Runnable() {
				
				@Override
				public void run() {
					try {
						log4j.info("=========> start " + iSitemap.getSource());
						iSitemap.start();
						log4j.info("=========> end " + iSitemap.getSource());
					} catch (Exception e) {
						log4j.info(e.getMessage(), e);
					} finally{
						log4j.info("=========> stop the scraper");
						iSitemap.markStop();
					}
					
				}
			});
		}
	}
	
	private void getPrice(Integer index){
		register();
		for (int i = 0; i < sitemaps.size(); i++) {
			if(index != null && i != index){
				continue;
			}
			sitemaps.get(i).finish();
		}
	}
	
	
}
